<?php


namespace app\general\controller;

use app\general\model\SqTraffic;
use think\Controller;

/**
 * Class Traffic
 * 自动增加访问量
 */
class Traffic extends Controller
{
    /**
     * 增加访问量 每小时
     */
    public function addVistit(){
        $sqTarffic=new SqTraffic();
        $info=$sqTarffic->where("id",1)->find();
        $id = $this->recordTimers(7);
        if($info){
            if($info["num"]>=30000000){
                $this->updateTimerType($id,"已经到达3000w");
                return false;
            }
            $sqTarffic->where("id",1)->setInc("num",50000);
        }else{
            $sqTarffic->save(['num'=>50000]);
        }
        $this->updateTimerType($id,"每小时增加访问量成功");
        return $this->success("添加成功");
    }
    /**
     * 每俩个小时
     */
    public function twoHourVisit(){
        $sqTarffic=new SqTraffic();
        $id = $this->recordTimers(7);
        $info=$sqTarffic->where("id",1)->find();
        if($info["num"]>=30000000){
            $this->updateTimerType($id,"已经到达3000w");
            return false;
        }
        $rand=rand(3,8);
        $sqTarffic->where("id",1)->setInc("num",$rand*100000);
        $this->updateTimerType($id,"每俩小时增加访问量成功");
        return $this->success("添加成功");
    }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }
}